﻿using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using SqlSugar;

namespace SalesManagementCommon.API.SalesManagementCommons.PurchaseInvoiceRead
{
    public class PurchaseInvoiceReadBackFillHandler : IRequestHandler<PurchaseInvoiceReadBackFillCommand, Result<PurchaseInvoiceReadBackFillDto>>
    {
        private readonly IBaseRepository<PurchaseInvoice> _purchaseInvoiceRepository;
        private readonly IBaseRepository<PurchaseInvoiceSubtable> _purchaseInvoiceSubtableRepository;
        private readonly ISqlSugarClient _sqlSugarClient;

        public PurchaseInvoiceReadBackFillHandler(IBaseRepository<PurchaseInvoice> purchaseInvoiceRepository, IBaseRepository<PurchaseInvoiceSubtable> purchaseInvoiceSubtableRepository, ISqlSugarClient sqlSugarClient)
        {
            _purchaseInvoiceRepository = purchaseInvoiceRepository;
            _purchaseInvoiceSubtableRepository = purchaseInvoiceSubtableRepository;
            _sqlSugarClient = sqlSugarClient;
        }



        /// <summary>
        /// 查看采购发票数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PurchaseInvoiceReadBackFillDto>> Handle(PurchaseInvoiceReadBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PurchaseInvoiceReadBackFillDto>();
            try
            {
                var purchaseInvoice = await (await _purchaseInvoiceRepository.GetAll()).ToListAsync();
                var purchaseInvoiceSubtable = await (await _purchaseInvoiceSubtableRepository.GetAll()).ToListAsync();
                var list = (from a in purchaseInvoice
                            where a.DocumentNo == request.DocumentNo
                            select new PurchaseInvoiceReadBackFillDto()
                            {
                                PurchaseInvoiceId = a.PurchaseInvoiceId,
                                SerialNumber = a.SerialNumber,
                                DocumentNo = a.DocumentNo,
                                BusinessType = a.BusinessType,
                                Vendor = a.Vendor,
                                BillingDate = a.BillingDate,
                                OnInvoice = a.OnInvoice,
                                DocumentDate = a.DocumentDate,
                                Operator = a.Operator,
                                TaxNo = a.TaxNo,
                                RedBasketLetter = a.RedBasketLetter,
                                XMLFormat = a.XMLFormat,
                                DocumentStatus = a.DocumentStatus,
                                Auditors = a.Auditors,
                                InvoiceTypeId = a.InvoiceTypeId,
                                InvoiceAmount = a.InvoiceAmount,
                                OFPFormat = a.OFPFormat,
                                BusinessStatusId = a.BusinessStatusId,
                                DateOfReview = a.DateOfReview,
                                InvoiceCode = a.InvoiceCode,
                                Tax = a.Tax,
                                InvoiceStatus = a.InvoiceStatus,
                                InvoiceNumber = a.InvoiceNumber,
                                PriceTaxTotal = a.PriceTaxTotal,
                                RemarkInfo = a.RemarkInfo,
                                purchaseInvoiceSubtableBackFillDtos = new List<PurchaseInvoiceSubtableBackFillDto>()

                            }).FirstOrDefault();
                if (list != null)
                {
                    list.purchaseInvoiceSubtableBackFillDtos = (from a in purchaseInvoiceSubtable
                                                                    //join b in purchaseReturnOrderGoods on a.MaterialNo equals b.MaterialCode
                                                                where a.SerialNumber == list.SerialNumber
                                                                select new PurchaseInvoiceSubtableBackFillDto()
                                                                {
                                                                    PurchaseInvoiceSubtableId = a.PurchaseInvoiceSubtableId,
                                                                    PurchaseON = a.PurchaseON,
                                                                    Warehouse = a.Warehouse,
                                                                    InventoryRN = a.InventoryRN,
                                                                    LogisticsCode = a.LogisticsCode,
                                                                    NameOfLogistics = a.NameOfLogistics,
                                                                    ModelSpecification = a.ModelSpecification,
                                                                    MainMetering = a.MainMetering,
                                                                    Num = a.Num,
                                                                    Price = a.Price,
                                                                    Amount = a.Amount,
                                                                    TaxRate = a.TaxRate,
                                                                    AmountOfTax = a.AmountOfTax,
                                                                    SerialNumber = a.SerialNumber,
                                                                }).ToList();
                }
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
